package org.example.travel_web.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.example.travel_web.dto.PageResult;
import org.example.travel_web.dto.attraction.AttractionDetail;
import org.example.travel_web.dto.attraction.UserAttractionPageResp;
import org.example.travel_web.dto.attraction.UserPageReq;
import org.example.travel_web.dto.category.PageResp;
import org.example.travel_web.dto.category.UserCatePageReq;
import org.example.travel_web.dto.comment.CommentReq;
import org.example.travel_web.dto.comment.UserCommentPageReq;
import org.example.travel_web.dto.comment.UserCommentPageResp;
import org.example.travel_web.dto.news.NewsDetail;
import org.example.travel_web.dto.news.UserNewsPageReq;
import org.example.travel_web.dto.news.UserNewsPageResp;
import org.example.travel_web.dto.route.RouteDetailResp;
import org.example.travel_web.dto.route.UserRoutePageReq;
import org.example.travel_web.dto.route.UserRoutePageResp;
import org.example.travel_web.entity.AttractionCategoriesET;
import org.example.travel_web.entity.NewsCategoriesET;
import org.example.travel_web.entity.RouteCategoriesET;
import org.example.travel_web.service.*;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/main")
@Tag(name = "首页")
public class MainController {

    @Resource
    private AttractionsService attractionsService;
    @Resource
    private NewsService newsService;
    @Resource
    private RoutesService routesService;
    @Resource
    private NewsCategoriesService newsCategoriesService;
    @Resource
    private AttractionCategoriesService attractionCategoriesService;
    @Resource
    private RouteCategoriesService routeCategoriesService;
    @Resource
    private CommentsService commentsService;

    @GetMapping("/category")
    @Operation(summary = "获取分类列表")
    public PageResult<PageResp> categoryPageResult(@Valid UserCatePageReq req) {
        switch (req.getType()) {
            case ATTRACTION -> {
                Page<AttractionCategoriesET> page = attractionCategoriesService.page(new Page<>(req.getPage(), req.getSize()));
                return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), BeanUtil.copyToList(page.getRecords(), PageResp.class));
            }
            case NEWS -> {
                Page<NewsCategoriesET> page = newsCategoriesService.page(new Page<>(req.getPage(), req.getSize()));
                return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), BeanUtil.copyToList(page.getRecords(), PageResp.class));
            }
            case ROUTE -> {
                Page<RouteCategoriesET> page = routeCategoriesService.page(new Page<>(req.getPage(), req.getSize()));
                return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), BeanUtil.copyToList(page.getRecords(), PageResp.class));
            }
            default -> {
                return PageResult.empty();
            }
        }
    }

    @GetMapping("/attraction")
    @Operation(summary = "景点分页")
    public PageResult<UserAttractionPageResp> attractionPageResult(@Valid UserPageReq req) {
        return attractionsService.userPageResult(req);
    }

    @GetMapping("/attraction/detail/{id}")
    @Operation(summary = "景点详情")
    @Parameter(name = "id", description = "景点id", required = true, in = ParameterIn.PATH)
    public AttractionDetail attractionDetail(@PathVariable Integer id) {
        return attractionsService.getAttractionDetail(id);
    }

    @GetMapping("/news")
    @Operation(summary = "资讯分页")
    public PageResult<UserNewsPageResp> newsPageResult(@Valid UserNewsPageReq req) {
        return newsService.userPageResult(req);
    }

    @GetMapping("/news/detail/{id}")
    @Operation(summary = "资讯详情")
    @Parameter(name = "id", description = "资讯id", required = true, in = ParameterIn.PATH)
    public NewsDetail newsDetail(@PathVariable Integer id) {
        return newsService.getNewsDetail(id);
    }

    @GetMapping("/route")
    @Operation(summary = "路线分页")
    public PageResult<UserRoutePageResp> routePageResult(@Valid UserRoutePageReq req) {
        return routesService.userPageResult(req);
    }

    @GetMapping("/route/detail/{id}")
    @Operation(summary = "路线详情")
    @Parameter(name = "id", description = "路线id", required = true, in = ParameterIn.PATH)
    public RouteDetailResp routeDetail(@PathVariable Integer id) {
        return routesService.getRouteDetail(id);
    }

    @GetMapping("/comment")
    @Operation(summary = "评论分页")
    public PageResult<UserCommentPageResp> commentPageResult(@Valid UserCommentPageReq req) {
        return commentsService.userCommentPageResult(req);
    }

    @PostMapping("/comment/add")
    @Operation(summary = "添加评论")
    @SaCheckLogin
    public void addComment(@Valid CommentReq req) {
        commentsService.addComment(req);
    }
}
